/*
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi non lorem
 * porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan. Etiam sed turpis ac
 * ipsum condimentum fringilla. Maecenas magna. Proin dapibus sapien vel ante. Aliquam erat
 * volutpat. Pellentesque sagittis ligula eget metus. Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.mrd.gtimpframe.action;

import java.util.Map;

import javax.annotation.Resource;

import com.mrd.constant.PlatformConstant;
import com.mrd.gtimpframe.entity.CompanyInfo;
import com.mrd.gtimpframe.entity.CustomCompanyInfo;
import com.mrd.gtimpframe.repository.CusInfoMapper;
import com.mrd.gtimpframe.service.CompanyInfoService;
import com.mrd.gtimpframe.service.CompanyService;
import com.mrd.util.LoginUtil;
import com.mrd.util.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrd.base.Jqgrid;
import com.mrd.gtimpframe.service.ChargingService;
import com.mrd.util.R;

/**
 * 计费控制层 Created by dengdc on 2017/8/10.
 */
@Controller
@RequestMapping("/fund/charging")
public class ChargingAction {

  @Resource
  private ChargingService chargingService;
  @Resource
  private CompanyInfoService companyInfoService;
  @Resource
  private CompanyService companyService;

  @RequestMapping("/query")
  @ResponseBody
  public R orderChargingQuery(@RequestBody Map<String, Object> params) {
    String comId= LoginUtil.getLoginUser().getCompany().getId();
    String comCustomCode = LoginUtil.getLoginUser().getCompany().getCom_customs_code();
    String supComId = params.get("supComId").toString();

    CompanyInfo companyInfo = (CompanyInfo)companyInfoService.selectByPrimaryKey(comId);

    String companyType = PlatformConstant.COM_TYPE_CUST.getKey();//公司类型
    String agentSelf = PlatformConstant.SELF_NO.getKey();//是否委托自己
    String supCustomCode = null;

    //判断是否待录单客户
    if(StringUtils.isNoneEmpty(companyInfo.getType(),companyInfo.getIsAgent(),companyInfo.getIsDrectCust())) {//根据逻辑判断
        if(companyInfo.getIsDrectCust().equals("1") & companyInfo.getType().equals("CL") && companyInfo.getIsAgent().equals("1"))
          companyType = PlatformConstant.COM_TYPE_HUNHE.getKey();
        //return "代录单人";
    }
    //supComId
    if(StringUtils.isNotEmpty(supComId)){
      supCustomCode = companyService.selectByPrimaryKey(supComId).getComCustomsCode();
    }

    //判断是否委托自己
    if(StringUtils.isNoneEmpty(comCustomCode,supCustomCode)&& comCustomCode.equals(supCustomCode)){
      agentSelf = PlatformConstant.SELF_YES.getKey();
    }

    params.put("customerId",comId);
    params.put("companyType",companyType);
    params.put("agentSelf",agentSelf);
    return chargingService.queryCharging(params);
  }

  /**
   * 报价、成本查询测试
   * 
   * @param jqgrid
   * @return
   */
  @RequestMapping("/test-query")
  @ResponseBody
  public Page testQuery(Jqgrid jqgrid) {
    return chargingService.testQuery(jqgrid);
  }
}
